<template>
<!-- 旅行延误 -->
    <el-card class="box-card">
        <div slot="header" class="card-heaer">
            <span style="line-height: 36px;">旅行延误理赔信息录入</span>
            <el-button type="primary" style="float:right" @click="up" v-if="state!='查看'&&state!='结果'">提交</el-button>
        </div>

        <el-form ref="form" :model="obj" :rules.sync="modelrules" label-width="180px">

            <el-form-item label="旅行使用的交通工具:" prop="BILL_TRAVELDELAY_WAY">
                <el-input v-model="obj.BILL_TRAVELDELAY_WAY" :disabled="disabled" style="float:left; width:200px; padding-left:5px"></el-input>
            </el-form-item>
            <el-form-item label="航班号/车次号:" prop="BILL_TRAVELDELAY_NUMBER">
                <el-input v-model="obj.BILL_TRAVELDELAY_NUMBER" :disabled="disabled" style="float:left; width:200px; padding-left:5px"></el-input>
            </el-form-item>
            <el-form-item label="出发地:" prop="BILL_TRAVELDELAY_FROM">
                <el-input v-model="obj.BILL_TRAVELDELAY_FROM" :disabled="disabled" style="float:left; width:200px; padding-left:5px"></el-input>
            </el-form-item>
            <el-form-item label="目的地:" prop="BILL_TRAVELDELAY_TO">
                <el-input v-model="obj.BILL_TRAVELDELAY_TO" :disabled="disabled" style="float:left; width:200px; padding-left:5px"></el-input>
            </el-form-item>
            <el-form-item label="原定出发时间:" prop="BILL_TRAVELDELAY_PLANOUT">
                <el-date-picker v-model="obj.BILL_TRAVELDELAY_PLANOUT" type="datetime" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="原定到达时间:" prop="BILL_TRAVELDELAY_PLANARBILLVE">
                <el-date-picker v-model="obj.BILL_TRAVELDELAY_PLANARBILLVE" type="datetime" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>

            </el-form-item>
            <el-form-item label="实际出发时间:" prop="BILL_TRAVELDELAY_FACTOUT">
                <el-date-picker v-model="obj.BILL_TRAVELDELAY_FACTOUT" type="datetime" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>

            </el-form-item>
            <el-form-item label="实际到达时间:" prop="BILL_TRAVELDELAY_FACTARBILLVE">
                <el-date-picker v-model="obj.BILL_TRAVELDELAY_FACTARBILLVE" type="datetime" :disabled="disabled" placeholder="选择日期时间">
                </el-date-picker>

            </el-form-item>
            <el-form-item label="延误总时间:">
                <el-input v-model="BILL_TRAVELDELAY_TALLTIME" :disabled="true" style="float:left; width:200px; padding-left:5px"></el-input>

            </el-form-item>
            <el-form-item label="旅行延误原因(详情描述)" prop="BILL_TRAVELDELAY_DETAILS">
                <el-input type="textarea" v-model="obj.BILL_TRAVELDELAY_DETAILS" :disabled="disabled"></el-input>
            </el-form-item>
            <el-form-item label="附件上传:" v-if="state!='理赔审核' && state!='查看' && state!='结果'">
                <el-button type="primary" class="fileselect">
                    <input type="file" @change="upload" value="" id="fileup"> 上传附件
                </el-button>
            </el-form-item>
            <el-form-item label="附件下载:" v-if="state=='理赔审核'||state=='结果' ||state=='查看'">
                <el-button type="primary" class="fileselect" @click="download">
                    下载附件
                </el-button>
            </el-form-item>

            <el-form-item label="审核:" v-if="state=='理赔审核'">
                <el-select placeholder="审核" v-model="BILL_DIED_STATE">
                    <el-option label="审核通过" value="需计算"></el-option>
                    <el-option label="退回重新填写" value="需理赔修改"></el-option>
                    <el-option label="拒赔" value="理赔审核不通过"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="不通过原因:" v-if="BILL_DIED_STATE=='理赔审核不通过'||BILL_DIED_STATE=='需理赔修改'|| state=='查看'||state=='修改'">
                <el-input type="textarea" v-model="BILL_DIED_WHY" :disabled="state=='查看'||state=='修改'"></el-input>
            </el-form-item>
        </el-form>
    </el-card>
</template>
<script>
import {
    mapState
} from 'vuex';
import getid from '@/js/getid';
export default {
    data() {
        return {
            showwhy: false,
            hospatialList: [],
            issuccess: false, //是否通过,
            disabled: false,
            obj: this.$store.state.lipeiDuty.travelDelay,
            ischeck: false,
            BILL_DIED_STATE: "",
            BILL_DIED_WHY: "",
            file: "",//文件地址
            BILL_TRAVELDELAY_TALLTIME: ""
        }
    },
    props: {
        state: {
            type: String,
            default: "新增"
        }
    },
    watch: {
        obj: {
            handler: function(val, oldval) {
                if (val.BILL_TRAVELDELAY_PLANOUT.length < 0 || val.BILL_TRAVELDELAY_PLANARBILLVE.length < 0 || val.BILL_TRAVELDELAY_FACTOUT.length < 0 || val.BILL_TRAVELDELAY_FACTARBILLVE.length < 0) {
                    return;
                }
                var time = this.timespan(val.BILL_TRAVELDELAY_PLANOUT, val.BILL_TRAVELDELAY_PLANARBILLVE, val.BILL_TRAVELDELAY_FACTOUT, val.BILL_TRAVELDELAY_FACTARBILLVE);

                this.BILL_TRAVELDELAY_TALLTIME = time;
            },
            deep: true
        }

    },
    computed: mapState({
        // 报案人信息
        modelrules: state => state.lipeiDuty.travelDelayRules,
        address: state => state.select.address,
    }),
    methods: {
        timespan(start, end, start1, end2) {

            var date1 = new Date(start);  //开始时间
            var date2 = new Date(start1);    //结束时间
            var date3 = (date2.getTime() - date1.getTime()) * 1;  //时间差的毫秒数

            var date11 = new Date(end);  //开始时间
            var date21 = new Date(end2);    //结束时间
            var date31 = (date21.getTime() - date11.getTime()) * 1; //时间差的毫秒数
            var date3 = date31 > date3 ? date31 : date3;
            //计算出相差天数
            var days = Math.floor(date3 / (24 * 3600 * 1000));

            //计算出小时数

            var leave1 = date3 % (24 * 3600 * 1000);  //计算天数后剩余的毫秒数
            var hours = Math.floor(leave1 / (3600 * 1000));
            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
            var minutes = Math.floor(leave2 / (60 * 1000));
            //计算相差秒数
            var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
            var seconds = Math.round(leave3 / 1000);
            var re = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";

            return re;
        },
        up() {
            if (this.state == "新增") {
                this.add();
            } else {
                this.changer();
            }

        },
        changer() {
            var model = JSON.parse(JSON.stringify(this.obj));
            if (this.state == "修改") {
                model.BILL_TRAVELDELAY_STATE = "需理赔审核";
                model.BILL_TRAVELDELAY_WHY = "";
            } else {
                model.BILL_TRAVELDELAY_STATE = this.BILL_DIED_STATE;
                model.BILL_TRAVELDELAY_WHY = this.BILL_DIED_WHY;
            }

            model.RI_TRAVELDELAY_ID = this.$route.query["id"];
            model.BILL_TRAVELDELAY_DOC = this.file;
            model.BILL_TRAVELDELAY_TALLTIME = this.BILL_TRAVELDELAY_TALLTIME;

            this.$http.put("BILL_TRAVELDELAY", model).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '审核成功',
                        message: '审核成功',
                        type: 'success'
                    });
                    this.$router.go(-1);
                } else {
                    this.$notify({
                        title: '提交失败',
                        message: '提交失败',
                        type: 'warning'
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });
        },
        add() {
            if (this.file == "") {
                this.$notify.error({
                    title: '数据不完整',
                    message: '请上传附件'
                });
                return;
            }
            var model = JSON.parse(JSON.stringify(this.obj));
            model.BILL_TRAVELDELAY_ID = getid.getid();
            model.BILL_TRAVELDELAY_STATE = "理赔审核";
            model.RI_TRAVELDELAY_ID = this.$route.query["id"];
            model.BILL_TRAVELDELAY_TALLTIME = this.BILL_TRAVELDELAY_TALLTIME;
            model.BILL_TRAVELDELAY_DOC = this.file;

            this.$http.post("BILL_TRAVELDELAY", model).then(response => {
                var data = response.body;
                if (data.code == 1) {
                    this.$notify({
                        title: '添加成功',
                        message: '添加成功',
                        type: 'success'
                    });
                    this.$router.go(-1);
                } else {
                    this.$notify({
                        title: '提交失败',
                        message: '提交失败',
                        type: 'warning'
                    });
                }
            }, response => {
                this.loading = false;
                this.$notify.error({
                    title: '网络错误',
                    message: '网络错误'
                });
            });
        },
        upload(event) {
            let file = event.target.files[0];
            // if (file.type != "application/x-zip-compressed") {
            //     this.$notify({
            //         title: '文件格式不正确',
            //         message: '仅支持zip格式文件',
            //         type: 'warning'
            //     });
            //     return;
            // }
            this.loading = true;
            if (file) {
                const formData = new FormData()
                formData.append('file', file)
                this.$http.post('ReportInfomation/file', formData, {
                    progress(event) {
                        //传递给父组件的progress方法

                        // self.$emit('progress', parseFloat(event.loaded / event.total * 100), flag)
                    }
                }).then(response => {
                    var data = response.body;
                    this.loading = false;
                    if (data.code == 1) {

                        this.file = data.msg[0];
                        this.$notify({
                            title: '附件上传成功',
                            message: '附件上传成功',
                            type: 'success'
                        });
                        document.getElementById("fileup").value = "";

                    } else {
                        this.loding = false;
                        this.$notify.error({
                            title: '提交失败',
                            message: data.msg
                        });
                        document.getElementById("fileup").value = "";
                    }
                }, response => {
                    this.loading = false;
                    this.$notify.error({
                        title: '网络错误',
                        message: '网络错误'
                    });
                    this.loding = false;
                    document.getElementById("fileup").value = "";
                })


            }
        },
        download() {
            var url = this.$http.options.root;
            var base = url.substring(0, url.length - 3);
            window.open(base + "Upload/" + this.file);
        }
    },

    activated() {

        this.file = "";
        this.BILL_DIED_STATE = "";
        this.BILL_DIED_WHY = "";
        var url;
        if (this.state == "新增") {
            url = "Lipei/GetByType?id=" + this.$route.query["rid"] + "&type=旅行延误";
        } else {
            url = "BILL_TRAVELDELAY/" + this.$route.query["id"];
        }
        this.$http.get(url).then(response => {
            var data = response.body;
            if (data.code == 1) {
                data.result[0].BILL_TRAVELDELAY_FACTARBILLVE = new Date((new Date(data.result[0].BILL_TRAVELDELAY_FACTARBILLVE) / 1000 + 28800) * 1000);
                data.result[0].BILL_TRAVELDELAY_FACTOUT = new Date((new Date(data.result[0].BILL_TRAVELDELAY_FACTOUT) / 1000 + 28800) * 1000);
                data.result[0].BILL_TRAVELDELAY_PLANARBILLVE = new Date((new Date(data.result[0].BILL_TRAVELDELAY_PLANARBILLVE) / 1000 + 28800) * 1000);
                data.result[0].BILL_TRAVELDELAY_PLANOUT = new Date((new Date(data.result[0].BILL_TRAVELDELAY_PLANOUT) / 1000 + 28800) * 1000);
                this.obj = data.result[0];
                this.BILL_DIED_WHY = data.result[0].BILL_TRAVELDELAY_WHY;
                this.file = data.result[0].BILL_TRAVELDELAY_DOC;



            } else {

            }
        }, response => {
            this.$notify.error({
                title: '网络错误',
                message: '网络错误'
            });
        });
    }


}
</script>
